home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / STRIPUNB.ICN < prev    next >
Text File  |  1992-09-28  |  4KB  |  131 lines

  1. ############################################################################
  2. #
  3. #    File:     stripunb.icn
  4. #
  5. #    Subject:  Procedure to strip unbalanced material
  6. #
  7. #    Author:   Richard L. Goerwitz
  8. #
  9. #    Date:     June 1, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #    Version:  1.6
  14. #
  15. ###########################################################################
  16. #  
  17. #     This procedure strips material from a line which is unbalanced with
  18. #  respect to the characters defined in arguments 1 and 2 (unbalanced
  19. #  being defined as bal() defines it, except that characters preceded
  20. #  by a backslash are counted as regular characters, and are not taken
  21. #  into account by the balancing algorithm).
  22. #
  23. #     One little bit of weirdness I added in is a table argument. Put
  24. #  simply, if you call stripunb() as follows,
  25. #
  26. #      stripunb('<','>',s,&null,&null,t)
  27. #
  28. #  and if t is a table having the form,
  29. #
  30. #      key:  "bold"        value: outstr("\e[2m", "\e1m")
  31. #      key:  "underline"   value: outstr("\e[4m", "\e1m")
  32. #      etc.
  33. #
  34. #  then every instance of "<bold>" in string s will be mapped to
  35. #  "\e2m," and every instance of "</bold>" will be mapped to "\e[1m."
  36. #  Values in table t must be records of type output(on, off).  When
  37. #  "</>" is encountered, stripunb will output the .off value for the
  38. #  preceding .on string encountered.
  39. #
  40. ############################################################################
  41. #
  42. #  Links: slashbal.icn
  43. #
  44. ############################################################################
  45.  
  46. link slashbal
  47.  
  48. global last_k
  49. record outstr(on, off)
  50.  
  51.  
  52. procedure stripunb(c1,c2,s,i,j,t)
  53.  
  54.     # NB:  Stripunb() returns a string - not an integer (like find,
  55.     # upto).
  56.  
  57.     local lookinfor, bothcs, s2, k, new_s
  58.     #global last_k
  59.     initial last_k := list()
  60.  
  61.     /c1 := '<'
  62.     /c2 := '>'
  63.     bothcs := c1 ++ c2
  64.     lookinfor := c1 ++ '\\'
  65.     c := &cset -- c1 -- c2
  66.  
  67.     /s := &subject
  68.     if \i then {
  69.     if i < 1 then
  70.         i := *s + (i+1)
  71.     }
  72.     else i := \&pos | 1
  73.     if \j then {
  74.     if j < 1 then
  75.         j := *s + (j+1)
  76.     }
  77.     else j := *s + 1
  78.  
  79.     s2 := ""
  80.     s ? {
  81.     while s2 ||:= tab(upto(lookinfor)) do {
  82.         if ="\\" then {
  83.         if not any(bothcs) then
  84.             s2 ||:= "\\"
  85.         &pos+1 > j & (return s2)
  86.         s2 ||:= move(1)
  87.         next
  88.         }
  89.         else {
  90.         &pos > j & (return s2)
  91.         any(c1) |
  92.             stop("stripunb:  Unbalanced string, pos(",&pos,").\n",s)
  93.         if not (k := tab(slashbal(c,c1,c2)))
  94.         then {
  95.             # If the last char on the line is the right-delim...
  96.             if (.&subject[&pos:0]||" ") ? slashbal(c,c1,c2)
  97.             # ...then, naturally, the rest of the line is the tag.
  98.             then k := tab(0)
  99.             else {
  100.             # BUT, if it's not the right-delim, then we have a
  101.             # tag split by a line break.  Blasted things.
  102.             return stripunb(c1,c2,&subject||read(&input),
  103.                     *.&subject,,t) |
  104.             # Can't find the right delimiter.  Parsing error.
  105.             stop("stripunb:  Incomplete tag\n",s[1:80] | s)
  106.             }
  107.         }
  108.         # T is the maptable.
  109.         if \t then {
  110.             k ?:= 2(tab(any(c1)), tab(upto(c2)), move(1), pos(0))
  111.             if k ?:= (="/", tab(0)) then {
  112.             compl:= pop(last_k) | stop("Incomplete tag, ",&subject) 
  113.             if k == ""
  114.             then k := compl
  115.             else k == compl | stop("Incorrectly paired tag,/tag.")
  116.             s2 ||:= \(\t[k]).off
  117.             }
  118.             else {
  119.             s2 ||:= \(\t[k]).on
  120.             push(last_k, k)
  121.             }
  122.         }
  123.         }
  124.     }
  125.     s2 ||:= tab(0)
  126.     }
  127.  
  128.     return s2
  129.  
  130. end
  131.